<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;
use App\Models\Cart;
use App\Services\GoodService;
use App\Services\GraspService;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CartController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     *
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('购物车同步')
            ->description('第三方购物车同步数据')
            ->body($this->grid());
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new Cart);
        $grid->header(function (){
            return view('admin.good.tips');
        });
        $grid->actions(function ($actions) {
            $actions->disableView();
            $actions->disableEdit();
        });
          
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->id('ID');
        $grid->item_id('dzy_id');
        $grid->title('商品名');
        $grid->other_id('other_id');
        $grid->isbn('isbn13');
        $grid->price('销售价格')->sortable();
        $grid->image('图片')->image(100, 100)->modal(function ($modal) {

            return "<img style='width:100%;' src='" . $this->image . "'>";
        });
        $grid->plan_remind('需要预选')->display(function ($plan){
           return $plan ? '否' : '是';
        });
        $grid->inStockCount('锁定')->display(function ($time){
           return $time ? date('Y-m-d H:i:s',$time) : '否';
        });

        $grid->created_at('创建时间');
        $grid->tools(function ($tools) {
            $button = '<a href="javascript:void(0)" id="dzySync" class="btn btn-sm btn-success pull-right">
                                <i class="fa fa-save"></i>&nbsp;&nbsp;同步
                            </a>';
            $tools->append($button);
        });
        $this->script();
        $grid->filter(function ($filter) {

            $filter->disableIdFilter();
            $filter->equal('item_id', '商品ID');
            $filter->equal('isbn', 'ISBN');
            $filter->like('title', '商品名');
            $filter->equal('plan_remind', '已预选')->radio(['0'=>'否','1'=>'是']);

        });
        return $grid;
    }

    protected function script()
    {
        $string = "$(\"#dzySync\").click(function () {
                dzySync();
            });";
        Admin::script($string);
    }

    public function post(Request $request)
    {
        $url               = $request->post('req_url');
        $headers           = $request->post('headers');
        $cookie            = '_ga=GA1.2.721106630.1571902246; _gid=GA1.2.358507308.1575862814; Hm_lvt_046a9150e95f58699927e236d330965a=1575596213,1575869962,1575949479,1576029002; _gat=1; Hm_lpvt_046a9150e95f58699927e236d330965a=1576134090; fish_c0="2|1:0|10:1576134084|7:fish_c0|24:MjEyMjU3OTAzMzEzMTU4MTk5|eff6332284ef9fedd6a2ab064cf0c9b8c86ff9d89fa4752ac2ea6d6b7685bcf5"';
        $headers['Cookie'] = $cookie;
        try {
            $data     = GraspService::exeHttp($headers, $url);

            $nextPage = $data['paging']['next'] ?? '';
            if (!$nextPage || empty($data['data'])) {
                return [
                    'status' => true,
                    'msg'    => 'success',
                    'data'   => [],
                ];
            }
            GoodService::insertCart($data['data']);
            $noStockCount = $data['noStockBookCount'] ?? 1;//没有锁定
            $inStockCount = $data['inStockBookCount'] ?? 1;//锁定
            $total = $noStockCount + $inStockCount;
            return [
                'status' => true,
                'msg'    => 'success',
                'data'   => ['next_url'=>$nextPage,'total'=>$total],
            ];

        } catch (\Exception $e) {
            Log::error("请求多抓鱼错误",[$e]);
            return [
                'status' => false,
                'msg'    => 'error',
                'data'   => [],
            ];
        }


    }
}
